(function ($) {
  $.fn.upload = function (look_call_back) {
    var mousedownTimeout;
    var rotateMap = {
      R0: 0,
      R360: 0,
      R90: 90,
      R180: 180,
      R270: 270,
    };

    var rotateDegMap = {
      0: 'R0',
      360: 'R0',
      '-360': 'R0',
      90: 'R90',
      '-270': 'R90',
      180: 'R180',
      '-180': 'R180',
      270: 'R270',
      '-90': 'R270',
    };
    var _this = $(this),
      item_html = "<li class='item'></li>",
      $add = $(
        '<li class="item add" title="上传文件"><svg class="icon" viewBox="0 0 1024 1024" version="1" xmlns="http://www.w3.org/2000/svg" width="200" height="200"><defs><style/></defs><path d="M737 436a174 174 0 0 1 172 172 172 172 0 0 1-172 172c-69 1-69 107 0 106 152-2 276-125 278-278S886 332 737 330c-69-1-69 105 0 106zM285 779a174 174 0 0 1-172-172 172 172 0 0 1 172-172c68-1 69-106 0-106A282 282 0 0 0 7 607a281 281 0 0 0 278 278c69 1 68-105 0-106z" fill="#4A5699"/><path d="M340 384a174 174 0 0 1 172-172 172 172 0 0 1 172 172c1 68 106 68 106 0a282 282 0 0 0-278-278 281 281 0 0 0-278 278c-1 68 105 68 106 0z" fill="#C45FA0"/><path d="M545 473c17 17 17 43 0 60L422 656a42 42 0 0 1-60-60l123-123c17-16 43-16 60 0z" fill="#F39A2B"/><path d="M485 473c17-16 44-16 60 0l123 123a42 42 0 0 1-60 60L485 533a42 42 0 0 1 0-60z" fill="#F39A2B"/><path d="M514 634c24 0 43 20 43 43v179a43 43 0 0 1-86 0V677c0-23 19-43 43-43z" fill="#E5594F"/></svg></li>'
      ),
      loading =
        '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200" class="svg-progress"><g fill="rgba(17,89,164,0.1)"><path d="M 0 70 Q 75 39, 150 70 T 300 70 T 450 70 T 600 70 T 750 70 V 320 H 0 V 0"></path><animateTransform attributeName="transform" attributeType="XML" type="translate" from="0" to="-300" dur="1.5s" repeatCount="indefinite"></animateTransform></g><g fill="rgba(17,89,164,0.15)"><path d="M 0 70 Q 87.5 47, 175 70 T 350 70 T 525 70 T 700 70 T 875 70 T 1050 70 V 320 H 0 V 0"></path><animateTransform attributeName="transform" attributeType="XML" type="translate" from="0" to="-350" dur="3s" repeatCount="indefinite"></animateTransform></g></svg><div class="progressnum"></div>',
      del =
        '<svg xmlns="http://www.w3.org/2000/svg" class="delete" title="删除" version="1" viewBox="0 0 1024 1024"><path fill="#fff" d="M512 70a439 439 0 0 1 442 442 439 439 0 0 1-442 442A439 439 0 0 1 70 512 439 439 0 0 1 512 70m0-40a482 482 0 1 0 0 964 482 482 0 0 0 0-964zm114 253v-1c0-21-17-38-38-38H436c-21 0-38 17-38 38v1H282v74h460v-74H626zM321 396v346c0 21 17 38 38 38h306c21 0 38-17 38-38V396H321zm114 306h-76V474h76v228zm115 0h-76V474h76v228zm115 0h-76V474h76v228z"/></svg>',
      look =
        '<svg xmlns="http://www.w3.org/2000/svg" class="look" version="1" viewBox="0 0 1024 1024"><path fill="#fff" d="M451 835a386 386 0 1 1 0-771 386 386 0 0 1 0 771zm0-675a291 291 0 1 0 0 581 291 291 0 0 0 0-581zm450 798c-15 0-30-5-42-17L658 740a58 58 0 0 1 83-82l201 201a58 58 0 0 1-41 99"/></svg>',
      left_rotation =
        '<svg t="1645671437937" class="left_rotation" title="逆时针旋转" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="7607" data-spm-anchor-id="a313x.7781069.0.i3" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><path d="M924.8 337.6c-22.6-53.4-54.9-101.3-96-142.4s-89-73.4-142.4-96C631.1 75.9 572.5 64 512 64S392.9 75.9 337.6 99.2c-53.4 22.6-101.3 54.9-142.4 96-22.4 22.4-42.2 46.8-59.2 73.1V228c0-19.8-16.2-36-36-36s-36 16.2-36 36v288c0 19.8 16.2 36 36 36s36-16.2 36-36v-50.2c4.2-34.8 13.2-68.7 27-101.2 19.1-45.1 46.4-85.6 81.2-120.4C279 209.4 319.5 182 364.6 163c46.7-19.7 96.3-29.8 147.4-29.8 51.2 0 100.8 10 147.4 29.8 45.1 19.1 85.6 46.4 120.4 81.2C814.6 279 842 319.5 861 364.6c19.7 46.7 29.8 96.3 29.8 147.4 0 51.2-10 100.8-29.8 147.4-19.1 45.1-46.4 85.6-81.2 120.4C745 814.6 704.5 842 659.4 861c-46.7 19.7-96.3 29.8-147.4 29.8-64.6 0-128.4-16.5-184.4-47.8-54.4-30.4-100.9-74.1-134.6-126.6-10.3-16.1-31.7-20.8-47.8-10.4-16.1 10.3-20.8 31.7-10.4 47.8 39.8 62 94.8 113.7 159.1 149.6 66.2 37 141.7 56.6 218.1 56.6 60.5 0 119.1-11.9 174.4-35.2 53.4-22.6 101.3-54.9 142.4-96 41.1-41.1 73.4-89 96-142.4C948.1 631.1 960 572.5 960 512s-11.9-119.1-35.2-174.4z" p-id="7608" data-spm-anchor-id="a313x.7781069.0.i0" class="" fill="#ffffff"></path><path d="M275.4 575.5c9.5-2.5 19.1 2.9 22.3 12.2 3.5 10.2 9.9 17.7 19.1 22.6 7.1 3.9 15.1 5.8 24 5.8 16.6 0 30.8-6.9 42.5-20.8 11.7-13.8 20-32.7 24.9-75.1-7.7 12.2-17.3 20.8-28.7 25.8-11.4 5-23.7 7.4-36.8 7.4-26.7 0-47.7-8.3-63.3-24.9-15.5-16.6-23.3-37.9-23.3-64.1 0-25.1 7.7-47.1 23-66.2 15.3-19 37.9-28.6 67.8-28.6 40.3 0 68.1 18.1 83.4 54.4 8.5 19.9 12.7 44.9 12.7 74.9 0 33.8-5.1 63.8-15.3 89.9-16.9 43.5-45.5 65.2-85.8 65.2-27 0-47.6-7.1-61.6-21.2-10-10.1-16.4-22-19.3-35.8-2-9.6 4-19.1 13.5-21.6l0.9 0.1z m103-74.4c9.4-7.5 14.1-20.6 14.1-39.3 0-16.8-4.2-29.3-12.7-37.5S360.6 412 347.5 412c-14 0-25.2 4.7-33.4 14.1-8.2 9.4-12.4 22-12.4 37.7 0 14.9 3.6 26.7 10.9 35.5 7.2 8.8 18.8 13.1 34.6 13.1 11.4 0 21.8-3.8 31.2-11.3zM646.6 414.4c12.4 22.8 18.5 54 18.5 93.7 0 37.6-5.6 68.7-16.8 93.3-16.2 35.3-42.8 52.9-79.6 52.9-33.2 0-57.9-14.4-74.2-43.3-13.5-24.1-20.3-56.4-20.3-97 0-31.4 4.1-58.4 12.2-80.9 15.2-42 42.7-63 82.5-63 35.9 0 61.8 14.8 77.7 44.3z m-40.2 173.3c9.4-13.9 14-39.9 14-78 0-27.4-3.4-50-10.1-67.7-6.8-17.7-19.9-26.6-39.4-26.6-17.9 0-31 8.4-39.3 25.2-8.3 16.8-12.4 41.6-12.4 74.3 0 24.6 2.6 44.4 7.9 59.4 8.1 22.8 22 34.3 41.6 34.3 15.7 0 28.3-7 37.7-20.9zM803.3 387.2c11.2 11.3 16.8 25 16.8 41.2 0 16.7-5.8 30.7-17.5 41.8C791 481.4 777.4 487 762 487c-17.1 0-31.2-5.8-42.1-17.4-10.9-11.6-16.4-25.1-16.4-40.6 0-16.5 5.8-30.4 17.3-41.7 11.5-11.3 25.3-17 41.2-17 16.3 0 30.1 5.7 41.3 16.9zM739.5 451c6.2 6.2 13.7 9.3 22.5 9.3 8.4 0 15.8-3.1 22.1-9.3 6.3-6.2 9.4-13.7 9.4-22.6 0-8.5-3.1-15.9-9.3-22.1-6.2-6.2-13.6-9.3-22.2-9.3s-16.1 3.1-22.4 9.3c-6.3 6.2-9.4 13.7-9.4 22.6-0.1 8.4 3 15.8 9.3 22.1z" p-id="7609" data-spm-anchor-id="a313x.7781069.0.i2" class="" fill="#ffffff"></path></svg>',
      right_rotation =
        '<svg t="1645671454380" class="right_rotation" title="顺时针旋转" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="7854" data-spm-anchor-id="a313x.7781069.0.i5" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><path d="M1464.3 279.7" p-id="7855"></path><path d="M512 960c-60.5 0-119.1-11.9-174.4-35.2-53.4-22.6-101.3-54.9-142.4-96s-73.4-89-96-142.4C75.9 631.1 64 572.5 64 512s11.9-119.1 35.2-174.4c22.6-53.4 54.9-101.3 96-142.4s89-73.4 142.4-96C392.9 75.9 451.5 64 512 64s119.1 11.9 174.4 35.2c53.4 22.6 101.3 54.9 142.4 96s73.4 89 96 142.4C948.1 392.9 960 451.5 960 512c0 19.1-15.5 34.6-34.6 34.6s-34.6-15.5-34.6-34.6c0-51.2-10-100.8-29.8-147.4-19.1-45.1-46.4-85.6-81.2-120.4C745 209.4 704.5 182 659.4 163c-46.7-19.7-96.3-29.8-147.4-29.8-51.2 0-100.8 10-147.4 29.8-45.1 19.1-85.6 46.4-120.4 81.2S182 319.5 163 364.6c-19.7 46.7-29.8 96.3-29.8 147.4 0 51.2 10 100.8 29.8 147.4 19.1 45.1 46.4 85.6 81.2 120.4C279 814.6 319.5 842 364.6 861c46.7 19.7 96.3 29.8 147.4 29.8 64.6 0 128.4-16.5 184.4-47.8 54.4-30.4 100.9-74.1 134.6-126.6 10.3-16.1 31.7-20.8 47.8-10.4 16.1 10.3 20.8 31.7 10.4 47.8-39.8 62-94.8 113.7-159.1 149.6-66.2 37-141.7 56.6-218.1 56.6z" p-id="7856" data-spm-anchor-id="a313x.7781069.0.i6" class="selected" fill="#ffffff"></path><path d="M924 552c-19.8 0-36-16.2-36-36V228c0-19.8 16.2-36 36-36s36 16.2 36 36v288c0 19.8-16.2 36-36 36zM275.4 575.5c9.5-2.5 19.1 2.9 22.3 12.2 3.5 10.2 9.9 17.7 19.1 22.6 7.1 3.9 15.1 5.8 24 5.8 16.6 0 30.8-6.9 42.5-20.8 11.7-13.8 20-32.7 24.9-75.1-7.7 12.2-17.3 20.8-28.7 25.8-11.4 5-23.7 7.4-36.8 7.4-26.7 0-47.7-8.3-63.3-24.9-15.5-16.6-23.3-37.9-23.3-64.1 0-25.1 7.7-47.1 23-66.2 15.3-19 37.9-28.6 67.8-28.6 40.3 0 68.1 18.1 83.4 54.4 8.5 19.9 12.7 44.9 12.7 74.9 0 33.8-5.1 63.8-15.3 89.9-16.9 43.5-45.5 65.2-85.8 65.2-27 0-47.6-7.1-61.6-21.2-10-10.1-16.4-22-19.3-35.8-2-9.6 4-19.1 13.5-21.6l0.9 0.1z m103-74.4c9.4-7.5 14.1-20.6 14.1-39.3 0-16.8-4.2-29.3-12.7-37.5S360.6 412 347.5 412c-14 0-25.2 4.7-33.4 14.1-8.2 9.4-12.4 22-12.4 37.7 0 14.9 3.6 26.7 10.9 35.5 7.2 8.8 18.8 13.1 34.6 13.1 11.4 0 21.8-3.8 31.2-11.3zM646.6 414.4c12.4 22.8 18.5 54 18.5 93.7 0 37.6-5.6 68.7-16.8 93.3-16.2 35.3-42.8 52.9-79.6 52.9-33.2 0-57.9-14.4-74.2-43.3-13.5-24.1-20.3-56.4-20.3-97 0-31.4 4.1-58.4 12.2-80.9 15.2-42 42.7-63 82.5-63 35.9 0 61.8 14.8 77.7 44.3z m-40.2 173.3c9.4-13.9 14-39.9 14-78 0-27.4-3.4-50-10.1-67.7-6.8-17.7-19.9-26.6-39.4-26.6-17.9 0-31 8.4-39.3 25.2-8.3 16.8-12.4 41.6-12.4 74.3 0 24.6 2.6 44.4 7.9 59.4 8.1 22.8 22 34.3 41.6 34.3 15.7 0 28.3-7 37.7-20.9zM803.3 387.2c11.2 11.3 16.8 25 16.8 41.2 0 16.7-5.8 30.7-17.5 41.8C791 481.4 777.4 487 762 487c-17.1 0-31.2-5.8-42.1-17.4-10.9-11.6-16.4-25.1-16.4-40.6 0-16.5 5.8-30.4 17.3-41.7 11.5-11.3 25.3-17 41.2-17 16.3 0 30.1 5.7 41.3 16.9zM739.5 451c6.2 6.2 13.7 9.3 22.5 9.3 8.4 0 15.8-3.1 22.1-9.3 6.3-6.2 9.4-13.7 9.4-22.6 0-8.5-3.1-15.9-9.3-22.1-6.2-6.2-13.6-9.3-22.2-9.3s-16.1 3.1-22.4 9.3c-6.3 6.2-9.4 13.7-9.4 22.6-0.1 8.4 3 15.8 9.3 22.1z" p-id="7857" data-spm-anchor-id="a313x.7781069.0.i7" class="selected" fill="#ffffff"></path></svg>',
      image_box = '<div class="item-image-box"></div>',
      $input = $('<input type="file" name="file" />'),
      $upload = $('<input type="hidden" />'),
      _moveDom,
      _x,
      _y,
      inX,
      inY,
      item_w,
      item_h,
      config = {
        height: _this.attr('data-height') ? _this.attr('data-height') : 0,
        width: _this.attr('data-width') ? _this.attr('data-width') : 1920,
        type: _this.attr('data-type')
          ? _this.attr('data-type')
          : 'png,jpg,jpeg,gif',
        upname: _this.attr('data-file') ? _this.attr('data-file') : 'file',
        mode: _this.attr('mode') ? _this.attr('mode') : '',
        inputname: _this.attr('data-name') ? _this.attr('data-name') : 'upload',
        num: _this.attr('data-num') ? _this.attr('data-num') : 10,
        action: _this.attr('action') ? _this.attr('action') : '/index.php',
        size: _this.attr('data-size') ? _this.attr('data-size') : 20480,
        value: _this.attr('data-value') ? _this.attr('data-value') : '',
        // 项目宽度
        itemWidth: _this.attr('item-width') ? _this.attr('item-width') : '',
        // 项目高度
        itemHeight: _this.attr('item-height') ? _this.attr('item-height') : '',
      };

    var is_multiple = config.num > 1;

    _this
      .append($add.attr('data-num', config.num).attr('data-type', config.type))
      .append($input.attr('multiple', config.num > 1 ? 'multiple' : false))
      .append($upload.attr('name', config.inputname))
      // .addClass(config.num > 1 ? 'multiple' : 'one')
      .addClass(is_multiple ? 'multiple' : 'qs-one')
      .on('click', 'li.add', function () {
        $input.click();
      })
      .on('change', "input[type='file']", function (e) {
        var files = e.target.files;
        var temp = [];
        for (var item in files) {
          if (typeof files[item] == 'object') {
            temp.push(files[item]);
          }
        }
        uploadfile(temp);
      })
      .on('click', 'li.error', function (e) {
        delfile($(e.currentTarget));
      })
      .on('click', 'svg.delete', function (e) {
        delfile($(e.currentTarget).parent('li.success'));
      })
      .on('click', 'svg.left_rotation', function (e) {
        rotationItem($(e.currentTarget).parent('li.success'), -90);
      })
      .on('click', 'svg.right_rotation', function (e) {
        rotationItem($(e.currentTarget).parent('li.success'), 90);
      })
      .on('click', 'svg.look', function (e) {
        if (look_call_back && typeof look_call_back == 'function') {
          look_call_back(
            _this,
            $(e.currentTarget).parent('li.success').data('url')
          );
        }
      })
      .on('mousedown', 'li.success', function (e) {
        if (!is_multiple) {
          return;
        }
        // 禁止右键
        if (e.originalEvent.button === 2) {
          return;
        }
        if (mousedownTimeout) {
          _moveDom && _moveDom.css('cursor', 'default');
          _moveDom = false;
          clearTimeout(mousedownTimeout);
        }
        var that = this;
        // 长按150ms 后才可以拖动
        mousedownTimeout = setTimeout(function () {
          _moveDom = $(that);
          _moveDom.css('cursor', 'move');

          var ethis = $(e.target);
          var offset = _moveDom.offset();
          var thisOffset = _this.offset();
          var left = offset.left;
          var top = offset.top;
          _x =
            parseInt(left) +
            (e.pageX - parseInt(left)) +
            (thisOffset.left - offset.left);
          _y =
            parseInt(top) +
            (e.pageY - parseInt(top)) +
            (thisOffset.top - offset.top);
          item_w =
            _moveDom.width() +
            2 +
            _moveDom.css('margin-right').replace('px', '') * 1;
          item_h =
            _moveDom.height() +
            2 +
            _moveDom.css('margin-bottom').replace('px', '') * 1;
          inX = false;
          inY = false;
        }, 150);
      });

    if (config.value) {
      //初始化
      var value = config.value.split(','),
        i = 0,
        html = '';
      var count = 0;
      var thumbnail = '_200x';
      var itemWidth = parseFloat(config.itemWidth);
      if (!isNaN(itemWidth) && itemWidth > 300) {
        thumbnail = '_500x';
      }

      for (var item in value) {
        if (i < config.num && value[i]) {
          var [url, urlFlag] = getURlFlag(value[i]);
          html = $("<li class='item success'></li>").append(del);

          if (config.itemWidth) {
            html.css('width', config.itemWidth);
          }
          if (config.itemHeight) {
            html.css('height', config.itemHeight);
          }

          // 是否图片
          if (isURLImg(url)) {
            html.append(image_box).append(left_rotation).append(right_rotation);
            html.addClass('is-image');
            html
              .children('.item-image-box')
              .css('background-image', `url(${url}!R0${thumbnail})`);
            if (config.itemWidth) {
              html.children('.item-image-box').css('width', config.itemWidth);
            }
            if (config.itemHeight) {
              html.children('.item-image-box').css('height', config.itemHeight);
            }
          } else {
            var icon = getfileicon(getURLExp(url));
            var file_name = getURLName(url);
            html
              .append(icon)
              .attr('title', file_name)
              .append("<div class='filename'>" + file_name + '</div>');
          }

          html
            .append(typeof look_call_back == 'function' ? look : '')
            .data('url', url)
            .attr('data-filename', hash(url))
            .insertBefore($add);
          _this.data('num', ++count).removeClass('empty');

          if (isURLImg(url)) {
            // 获url度数
            var deg = rotateMap[urlFlag] || 0;
            rotationItem(html, deg);
          } else {
            setfilelist();
          }
        }
        i++;
      }
    } else {
      _this.data('num', 0).addClass('empty');
    }

    $(document)
      .on('mousemove', function (e) {
        if (_moveDom) {
          var x = e.pageX - _x;
          var y = e.pageY - _y;
          var thisI = _moveDom.index();
          var offset = _this.offset();
          var qh = _this.height() ? _this.height() : 134;
          _moveDom.css({ top: y, left: x, zIndex: 999, position: 'absolute' });
          inX = e.pageX - offset.left;
          inY = e.pageY - offset.top;
          if (inY > qh + 20 || inY < 0) {
            _moveDom.addClass('delete');
          } else {
            _moveDom.removeClass('delete');
          }
          inX = parseInt(inX / item_w);
          inY = parseInt(inY / item_h);
          inX = parseInt(inX + (_this.width() / item_w) * inY);
          if (inX >= thisI) inX++;
          _this.children('li').not(_moveDom).css('margin-left', 0);
          _this.children('li').eq(inX).not(_moveDom).css('margin-left', item_w);
        }
      })
      .on('mouseup', function () {
        if (_moveDom) {
          _moveDom.css('cursor', 'default');

          if (_moveDom.hasClass('delete') && _moveDom.data('filename')) {
            delfile(_moveDom);
            _moveDom = false;
            _this.children('li').not('.add').css({
              position: 'relative',
              top: 0,
              left: 0,
              'margin-left': 0,
              zIndex: 0,
            });
            _this.children('li.add').css('margin-left', 0);
            return false;
          }
          var newImg = _moveDom.clone();
          newImg.data('url', _moveDom.data('url'));
          if (inX !== false) {
            if (inX >= _this.children('li').length) newImg.insertBefore($add);
            else newImg.insertBefore(_this.children('li').eq(inX));
            _moveDom.remove();
            setfilelist();
            _this.children('li').not('.add').css({
              position: 'relative',
              top: 0,
              left: 0,
              'margin-left': 0,
              zIndex: 0,
            });
            _this.children('li.add').css('margin-left', 0);
          }
        }
        mousedownTimeout && clearTimeout(mousedownTimeout);
        _moveDom = false;
      })
      .on('dragenter', function (event) {
        event.originalEvent.preventDefault();
      })
      .on('dragover', function (event) {
        event.originalEvent.preventDefault();
        if ($(event.target).hasClass('upload')) _this.addClass('dragenter');
        else _this.removeClass('dragenter');
      })
      .on('drop', function (event) {
        event.originalEvent.preventDefault();
        _this.removeClass('dragenter');

        var files = event.originalEvent.dataTransfer.files;
        var temp = [];
        for (var item in files) {
          if (typeof files[item] == 'object') {
            temp.push(files[item]);
          }
        }
        uploadfile(temp);
        return false;
      });

    function beforeUpload({ filename, isMultiple, fileTotal, callback } = {}) {
      if (
        config.mode !== 'local' &&
        UploadHelper &&
        UploadHelper.getUploadXhr
      ) {
        UploadHelper.getUploadXhr({
          filename,
          isMultiple,
          fileTotal,
          callback,
        });
      } else {
        callback();
      }
    }

    async function uploadfile(e) {
      var fileObj = e;

      if (
        config.mode !== 'local' &&
        UploadHelper &&
        UploadHelper.advanceCacheResult
      ) {
        await UploadHelper.advanceCacheResult();
      }

      for (var item in fileObj) {
        if (typeof fileObj[item] == 'object') {
          var rs = addfile(fileObj[item], function (data, item, file) {
            item.insertBefore($add);
            var formData = new FormData();

            _this.removeClass('empty');

            beforeUpload({
              filename: file.name,
              isMultiple: fileObj.length > 1,
              fileTotal: fileObj.length,
              callback: function (xhrData) {
                const uploadData = {
                  action: config.action,
                  formData: formData,
                  ...(xhrData || {}),
                };

                const _formData = uploadData.formData;
                _formData.append(config.upname, data, file.name);

                $.ajax({
                  url: uploadData.action,
                  type: 'POST',
                  data: _formData,
                  dataType: 'text',
                  processData: false,
                  contentType: false,
                  success: function (data) {
                    try {
                      data = JSON.parse(data);
                    } catch (e) {
                      data = { code: 0 };
                    }
                    item.children('.svg-progress').remove();
                    item.children('.progressnum').remove();
                    if (data.code || data.code === 0) {
                      item.addClass('success').append(del);

                      // 是否图片
                      if (isURLImg(file.name)) {
                        item
                          .addClass('is-image')
                          .append(left_rotation)
                          .append(right_rotation);
                      }

                      item
                        .append(typeof look_call_back == 'function' ? look : '')
                        .data('url', uploadData.fileUrl || data.msg);
                    } else {
                      item
                        .addClass('error')
                        .attr(
                          'data-error',
                          data.msg ? data.msg : '服务端返回数据异常'
                        );
                    }
                    setfilelist();
                  },
                  xhr: function () {
                    var xhr = new XMLHttpRequest();
                    xhr.upload.addEventListener(
                      'progress',
                      function (evt) {
                        if (evt.lengthComputable) {
                          var _total = evt.total + 1;
                          var percentComplete = Math.floor(
                            (evt.loaded * 100) / _total
                          );
                          item
                            .children('.svg-progress')
                            .css(
                              'height',
                              50 + (120 * percentComplete) / 100 + '%'
                            );
                          item
                            .children('.progressnum')
                            .text(percentComplete + '%');
                        }
                      },
                      false
                    );
                    return xhr;
                  },
                  error: function () {
                    item.addClass('error').attr('data-error', '网络连接异常！');
                    setfilelist();
                  },
                });
              },
            });
          });
        }
      }
    }

    function addfile(file, callback) {
      //验证文件
      $input.val('');
      if (config.num == 1) {
        _this.children('.item').not('.add').remove();
      } else {
        if (config.num <= _this.data('num')) return false;
        _this.data('num', _this.data('num') + 1);
      }

      var size = file.size,
        exp = file.name.toLowerCase().split('.').splice(-1).join(),
        html = $(item_html);

      if (config.itemWidth) {
        html.css('width', config.itemWidth);
      }
      if (config.itemHeight) {
        html.css('height', config.itemHeight);
      }

      //验证文件类型
      var yexp = config.type.split(',');
      if (yexp.indexOf(exp) < 0) {
        var errorstr = '不能上传.' + exp + '的文件!';
      }
      if (
        ['png', 'jpg', 'jpeg', 'gif', 'bmp'].indexOf(exp) < 0 &&
        size > config.size * 1024
      ) {
        var errorstr = '不能上传大于.' + config.size + 'KB 的文件!';
      }
      if (errorstr) {
        html
          .append(getfileicon(exp))
          .addClass('error')
          .append("<div class='filename'>" + file.name + '</div>')
          .attr('data-error', errorstr)
          .insertBefore($add);
        _this.removeClass('empty');
        setfilelist();
        return false;
      }

      var r = new FileReader();
      r.readAsDataURL(file);
      r.onload = function () {
        var thishash =
          hash(file.name) +
          hash(file.type) +
          hash(file.size.toString()) +
          hash(r.result);
        if (_this.children('li[data-filename="' + thishash + '"]').length > 0) {
          return false;
        }
        html.attr('data-filename', thishash);
        if (['png', 'jpg', 'jpeg', 'gif', 'bmp'].indexOf(exp) >= 0) {
          var image = new Image();
          image.src = r.result;
          image.onload = function () {
            var that = this;
            // 默认按比例压缩
            var w = that.width,
              h = that.height,
              scale = w / h;
            if (config.width && config.width < w) {
              w = config.width;
              h = w / scale;
            } else if (config.height && h > config.height) {
              h = config.height;
              w = h * scale;
            }
            var quality = 1; // 默认图片质量为0.7
            var canvas = document.createElement('canvas');
            var ctx = canvas.getContext('2d');
            var anw = document.createAttribute('width');
            anw.nodeValue = w;
            var anh = document.createAttribute('height');
            anh.nodeValue = h;
            canvas.setAttributeNode(anw);
            canvas.setAttributeNode(anh);
            ctx.drawImage(that, 0, 0, w, h);
            // quality值越小，所绘制出的图像越模糊
            var base64 = canvas.toDataURL('image/jpeg', quality);
            html
              .append(image_box)
              .children('.item-image-box')
              .css('background-image', "url('" + base64 + "')");
            if (config.itemWidth) {
              html.children('.item-image-box').css('width', config.itemWidth);
            }
            if (config.itemHeight) {
              html.children('.item-image-box').css('height', config.itemHeight);
            }
            html.addClass('is-image').append(loading);

            //开始上传
            var uploadFile = convertBase64UrlToBlob(base64);
            callback(uploadFile, html, file);
            return;
          };
        } else {
          var icon = getfileicon(exp);
          html
            .attr('title', file.name)
            .append(icon)
            .append(
              '<div class="filename" title="' +
                file.name +
                '">' +
                file.name +
                '</div>'
            )
            .append(loading);
          var uploadFile = file;
          callback(uploadFile, html, file);
          return;
        }
      };
    }

    function convertBase64UrlToBlob(urlData) {
      var arr = urlData.split(','),
        mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]),
        n = bstr.length,
        u8arr = new Uint8Array(n);
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
      }
      return new Blob([u8arr], { type: mime });
    }

    function hash(input) {
      var hash = 5381,
        I64BIT_TABLE =
          'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'.split(
            ''
          ),
        i = input.length - 1;
      if (typeof input == 'string') {
        for (; i > -1; i--) hash += (hash << 5) + input.charCodeAt(i);
      } else {
        for (; i > -1; i--) hash += (hash << 5) + input[i];
      }
      var value = hash & 0x7fffffff;
      var retValue = '';
      do {
        retValue += I64BIT_TABLE[value & 0x3f];
      } while ((value >>= 6));
      return retValue;
    }

    function getfileicon(type) {
      if (['asp', 'php', 'js', 'java', 'html', 'css', 'sql'].indexOf(type) >= 0)
        return '<svg xmlns="http://www.w3.org/2000/svg" class="icon" version="1" viewBox="0 0 1024 1024"><defs/><path fill="#FF8976" d="M160 32a49 49 0 0 0-48 48v864c0 12 5 25 14 34 10 9 22 14 34 14h704c12 0 25-5 34-14 9-10 14-22 14-34V304L640 32H160z"/><path fill="#FFD0C8" d="M912 304H688c-12 0-25-5-34-14s-14-22-14-34V32l272 272z"/><path fill="#FFF" d="M422 564l-118 46 118 47v37l-163-65v-37l163-65v37zm116-106h37l-89 240h-37l89-240zm64 200l118-47-118-46v-37l163 64v38l-163 64v-36z"/></svg>';
      if (['psb', 'psd'].indexOf(type) >= 0)
        return '<svg xmlns="http://www.w3.org/2000/svg" class="icon" version="1" viewBox="0 0 1024 1024"><path fill="#8095FF" d="M168 32c-12 0-25 5-34 14s-14 22-14 34v864c0 12 5 25 14 34 10 9 22 14 34 14h704c12 0 25-5 34-14 9-10 14-22 14-34V304L648 32H168z"/><path fill="#CCD5FF" d="M920 304H696c-12 0-25-5-34-14s-14-22-14-34V32l272 272z"/><path fill="#C0CAFF" d="M504 550c5-2 10-4 16-4s11 2 16 4l185 108c5 2 8 8 8 13s-3 11-8 14L534 793c-4 2-10 4-16 4s-11-2-16-4L318 686c-5-3-8-8-8-14s3-11 8-14l186-108z"/><path fill="#FFF" d="M504 390c5-2 10-4 16-4s11 2 16 4l185 108c5 2 8 8 8 13s-3 11-8 14L534 633c-4 2-10 4-16 4s-11-2-16-4L318 526c-5-3-8-8-8-14s3-11 8-14l186-108z"/></svg>';
      if (['xls', 'xlsx', 'number', 'et', 'ett'].indexOf(type) >= 0)
        return '<svg xmlns="http://www.w3.org/2000/svg" class="icon" version="1" viewBox="0 0 1024 1024"><path fill="#5ACC9B" d="M160 32a49 49 0 0 0-48 48v864c0 12 5 25 14 34 10 9 22 14 34 14h704c12 0 25-5 34-14 9-10 14-22 14-34V304L640 32H160z"/><path fill="#BDEBD7" d="M912 304H688c-12 0-25-5-34-14s-14-22-14-34V32l272 272z"/><path fill="#FFF" d="M475 538L366 386h76l71 108 74-108h73L549 538l117 161h-76l-79-115-78 116h-75l117-162z"/></svg>';
      if (['wps', 'wpt', 'page', 'doc', 'docx'].indexOf(type) >= 0)
        return '<svg xmlns="http://www.w3.org/2000/svg" class="icon" version="1" viewBox="0 0 1024 1024"><path fill="#6CCBFF" d="M160 32a49 49 0 0 0-48 48v864c0 12 5 25 14 34 10 9 22 14 34 14h704c12 0 25-5 34-14 9-10 14-22 14-34V304L640 32H160z"/><path fill="#C4EAFF" d="M912 304H688c-12 0-25-5-34-14s-14-22-14-34V32l272 272z"/><path fill="#FFF" d="M280 386h65l65 244 72-244h62l72 244 66-244h62l-96 314h-65l-71-242h-1l-72 241h-65l-94-313z"/></svg>';
      if (['ppt', 'pptx', 'key', 'dps', 'dpt', 'wpp'].indexOf(type) >= 0)
        return '<svg xmlns="http://www.w3.org/2000/svg" class="icon" version="1" viewBox="0 0 1024 1024"><path fill="#FF8976" d="M160 32a49 49 0 0 0-48 48v864c0 12 5 25 14 34 10 9 22 14 34 14h704c12 0 25-5 34-14 9-10 14-22 14-34V304L640 32H160z"/><path fill="#FFD0C8" d="M912 304H688c-12 0-25-5-34-14s-14-22-14-34V32l272 272z"/><path fill="#FFF" d="M386 386h176c70 0 92 47 92 97 0 48-28 97-92 97H446v120h-60V386zm60 145h96c35 0 53-10 53-47 0-38-25-48-48-48H446v95z"/></svg>';
      if (type == 'pdf')
        return '<svg xmlns="http://www.w3.org/2000/svg" class="icon" version="1" viewBox="0 0 1024 1024"><path fill="#FF5562" d="M160 32a49 49 0 0 0-48 48v864c0 12 5 25 14 34 10 9 22 14 34 14h704c12 0 25-5 34-14 9-10 14-22 14-34V304L640 32H160z"/><path fill="#FFBBC0" d="M912 304H688c-12 0-25-5-34-14s-14-22-14-34V32l272 272z"/><path fill="#FFF" d="M696 843c-50 0-95-86-119-142-40-17-84-32-127-43-37 25-100 62-149 62-31 0-52-15-60-42-7-21-1-36 5-44 13-18 40-27 80-27 32 0 72 6 118 17 30-21 59-45 86-70-12-56-25-147 8-188 16-20 40-27 70-18 33 10 45 30 49 45 13 54-49 128-91 171 9 38 21 77 36 113 61 27 133 67 141 111 3 15-1 30-13 42-11 8-22 13-34 13zm-74-121c30 61 59 90 74 90 2 0 6-1 10-5 6-5 6-9 5-13-3-16-29-42-89-72zm-296-83c-40 0-51 10-54 14-1 1-4 5-1 17 3 9 9 19 30 19 25 0 62-15 105-40-31-7-58-10-80-10zm158-5c26 8 52 16 77 26-9-23-16-47-23-70l-54 44zm99-258c-9 0-15 3-21 10-16 20-18 73-5 140 49-52 75-100 69-125-1-4-4-15-27-22l-16-3z"/></svg>';
      if (type == 'txt')
        return '<svg xmlns="http://www.w3.org/2000/svg" class="icon" version="1" viewBox="0 0 1024 1024"><path fill="#E5E5E5" d="M160 32a49 49 0 0 0-48 48v864c0 12 5 25 14 34 10 9 22 14 34 14h704c12 0 25-5 34-14 9-10 14-22 14-34V304L640 32H160z"/><path fill="#CCC" d="M912 304H688c-12 0-25-5-34-14s-14-22-14-34V32l272 272z"/><path fill="#FFF" d="M264 376h208c14 0 24-10 24-24s-10-24-24-24H264c-14 0-24 10-24 24s10 24 24 24zm0 160h496c14 0 24-10 24-24s-10-24-24-24H264c-14 0-24 10-24 24s10 24 24 24zm496 112H264c-14 0-24 10-24 24s10 24 24 24h496c14 0 24-10 24-24s-10-24-24-24z"/></svg>';
      if (
        ['zip', 'rar', 'gzip', '7-zip', 'zipz', 'rarr', 'iso'].indexOf(type) >=
        0
      )
        return '<svg xmlns="http://www.w3.org/2000/svg" class="icon" version="1" viewBox="0 0 1024 1024"><path fill="#5ACC9B" d="M944 944H80c-26 0-48-18-48-41V656h960v247c0 23-22 41-48 41z"/><path fill="#6CCBFF" d="M80 80h864c26 0 48 18 48 41v247H32V121c0-23 22-41 48-41z"/><path fill="#FFD766" d="M32 368h960v288H32z"/><path fill="#FF5562" d="M352 80h320v864H352z"/><path fill="#FFF" d="M444 128h64v48h-64zm64-48h64v48h-64zm0 96h64v48h-64zm-64 48h64v48h-64zm64 48h64v48h-64zm-64 48h64v48h-64zm64 48h64v48h-64zm-64 48h64v48h-64zm64 48h64v48h-64zm-64 48h64v48h-64zm64 48h64v48h-64zm-64 48h64v48h-64zm64 48h64v48h-64zm-64 48h64v48h-64zm64 48h64v48h-64zm-64 48h64v48h-64zm0 96h64v48h-64zm64-48h64v48h-64z"/></svg>';
      if (
        [
          'avi',
          'wmv',
          'mpeg',
          'mp4',
          'mov',
          'mkv',
          'flv',
          'f4v',
          'm4v',
          'rmvb',
          'rm',
          '3gp',
          'dat',
          'ts',
          'mts',
          'vob',
        ].indexOf(type) >= 0
      )
        return '<svg xmlns="http://www.w3.org/2000/svg" class="icon" version="1" viewBox="0 0 1024 1024"><path fill="#8095FF" d="M80 34h864v960H80z"/><path fill="#FFF" d="M136 112a40 40 0 1 0 80 0 40 40 0 1 0-80 0zM136 272a40 40 0 1 0 80 0 40 40 0 1 0-80 0zM136 432a40 40 0 1 0 80 0 40 40 0 1 0-80 0zM136 592a40 40 0 1 0 80 0 40 40 0 1 0-80 0zM136 752a40 40 0 1 0 80 0 40 40 0 1 0-80 0zM136 912a40 40 0 1 0 80 0 40 40 0 1 0-80 0zM824 112a40 40 0 1 0 80 0 40 40 0 1 0-80 0zM824 272a40 40 0 1 0 80 0 40 40 0 1 0-80 0zM824 432a40 40 0 1 0 80 0 40 40 0 1 0-80 0zM824 592a40 40 0 1 0 80 0 40 40 0 1 0-80 0zM824 752a40 40 0 1 0 80 0 40 40 0 1 0-80 0zM824 912a40 40 0 1 0 80 0 40 40 0 1 0-80 0zM648 508L436 362c-5-3-11-4-17 0-5 3-9 8-9 14v290c0 6 4 12 9 15 6 3 12 2 17-1l212-146c5-3 7-8 7-13s-3-10-7-13z"/></svg>';
      if (['gif', 'jpg', 'jpeg', 'png', 'bmp'].indexOf(type) >= 0)
        return '<svg xmlns="http://www.w3.org/2000/svg" class="icon" version="1" viewBox="0 0 1024 1024"><defs/><path fill="#FF5562" d="M160 32a49 49 0 0 0-48 48v864c0 12 5 25 14 34 10 9 22 14 34 14h704c12 0 25-5 34-14 9-10 14-22 14-34V304L640 32H160z"/><path fill="#FFBBC0" d="M912 304H688c-12 0-25-5-34-14s-14-22-14-34V32l272 272z"/><path fill="#FFF" d="M758 706L658 550c-3-4-8-7-13-7s-11 3-14 7l-53 84-120-195c-4-5-8-7-14-7s-10 3-14 7L266 706c-4 4-4 11 0 16 3 5 8 8 13 8h466c5 0 11-4 14-8 3-6 3-12-1-16zM622 412a40 40 0 1 0 80 0 40 40 0 1 0-80 0z"/></svg>';
      return '<svg xmlns="http://www.w3.org/2000/svg" class="icon" version="1" viewBox="0 0 1024 1024"><path fill="#E5E5E5" d="M160 32a49 49 0 0 0-48 48v864c0 12 5 25 14 34 10 9 22 14 34 14h704c12 0 25-5 34-14 9-10 14-22 14-34V304L640 32H160z"/><path fill="#CCC" d="M912 304H688c-12 0-25-5-34-14s-14-22-14-34V32l272 272z"/></svg>';
    }

    function setfilelist() {
      var filelist = [];
      _this.children('li.success').each(function () {
        filelist.push($(this).data('url'));
      });
      if (
        _this.children('li').not('.add').length >= config.num &&
        config.num > 1
      ) {
        $add.hide();
      } else {
        $add.show();
      }
      if (_this.children('li').not('.add').length === 0) {
        _this.addClass('empty');
      }
      // console.log(filelist);
      _this.val(filelist.join(','));
    }

    function delfile(item) {
      item.fadeOut(333, function () {
        item.remove();
        setfilelist();
      });
      _this.data('num', _this.data('num') - 1);
    }

    function getURLExp(url) {
      return url.toLowerCase().split('.').splice(-1).join();
    }

    function isURLImg(url) {
      var exp = getURLExp(url);
      return ['png', 'jpg', 'jpeg', 'gif', 'bmp'].indexOf(exp) > -1;
    }

    function getURLName(url) {
      return url.split('/').splice(-1).join();
    }

    function getURlFlag(url) {
      url = url.replace(/\@/g, '!');
      var urls = url.split('!');
      var flag = '';
      if (urls.length > 1) {
        flag = urls.pop();
      }
      return [urls.join('!'), flag];
    }

    function rotationItem(item, deg, init) {
      var url = item.data('url');
      if (!init) {
        deg = (item.data('rotate-deg') || 0) + deg;
        if (deg === 360 || deg === -360) {
          deg = 0;
        }
      }
      item.data('rotate-deg', deg);

      if (url) {
        url = getURlFlag(url)[0];
        if (deg !== 0) {
          var degFlag = rotateDegMap[deg];
          url = getURlFlag(url)[0];
          url = url + '!' + degFlag;
        }
        item.data('url', url);
      }
      item.children('.item-image-box').css('transform', `rotate(${deg}deg)`);

      // 刷新数据
      setfilelist();
    }
  };
})(jQuery);
